import axios from '../AxiosConfig.js'

export const createQueue = data => {
  return axios({
    method: 'put',
    url: '/api/queues/' + encodeURIComponent(data.vhost) +'/' + data.name,
    data: data
  })
}

export const deleteQueue = (vhost, name) => {
  return axios({
    method: 'delete',
    url: '/api/queues/' + encodeURIComponent(vhost) +'/' + name,
  })
}

export const deleteQueueContents = (vhost, name) => {
  return axios({
    method: 'delete',
    url: '/api/queues/' + encodeURIComponent(vhost) +'/' + name + '/contents',
  })
}

export const selectQueue = (vhost, name, msgRates) => {
  let split = msgRates.split("_");
  return axios({
    method: 'get',
    url: '/api/queues/' + encodeURIComponent(vhost) +'/' + name,
    params: {
      "lengths_age": split[0],
      "lengths_incr": split[1],

      "msg_rates_age": 60,
      "msg_rates_incr": 5,
    }
  })
}

export const listQueue = (name) => {
  if (name === undefined) {
    name = ""
  }
  return axios({
    method: 'get',
    url: '/api/queues',
    params: {
      "page": 1,
      "page_size": 500,
      "name": name,
      "use_regex": false,
      "pagination": true
    }
  })
}

export const listQueueByVhost = (vhost) => {
  if (vhost === undefined || vhost === '') {
    vhost = "/"
  }
  return axios({
    method: 'get',
    url: '/api/queues/' + encodeURIComponent(vhost)
  })
}

export const listQueueBinding = (vhost, name) => {
  return axios({
    method: 'get',
    url: '/api/queues/' + encodeURIComponent(vhost) + '/' + name + '/bindings',
  })
}

export const listQueueContents = (data) => {
  return axios({
    method: 'post',
    url: '/api/queues/' + encodeURIComponent(data.vhost) + '/' + data.name + '/get',
    data: {
      "count": data.count,
      "ackmode": data.ackmode,
      "encoding": data.encoding,
      "truncate": data.truncate
    }
  })
}

